package com.hxrd.androidprint;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.barcode.Scanner;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.hxrd.androidprint.DBUtils.DBUtils;
import com.hxrd.androidprint.fragment.SendFirstFragment;
import com.hxrd.androidprint.fragment.SendForthFragment;
import com.hxrd.androidprint.fragment.SendSecondFragment;
import com.hxrd.androidprint.fragment.SendThirdFragment;

import hardware.print.printer;

public class WarehouseSendGoodsActivity extends FragmentActivity {

    private CallBackFirst callBackFirst;
    private CallBackSecond callBackSecond;
    private CallBackThird callBackThird;
    private CallBackForth callBackForth;

    private String m_Broadcastname;
    private String name,urlIp;

    private Fragment firstFragment, secondFragment, thirdFragment, forthFragment, mFragment;
    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_warehouse_send_goods);

        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        urlIp = intent.getStringExtra("urlIp");
        Log.e("发货界面ip", urlIp);

        Bundle bundle=new Bundle();
        bundle.putString("name",name);
        bundle.putString("urlIp",urlIp);

        radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        firstFragment = new SendFirstFragment();
        secondFragment = new SendSecondFragment();
        thirdFragment = new SendThirdFragment();
        forthFragment = new SendForthFragment();

        firstFragment.setArguments(bundle);
        secondFragment.setArguments(bundle);
        thirdFragment.setArguments(bundle);
        forthFragment.setArguments(bundle);

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().add(R.id.main_fragment, firstFragment).commit();
        mFragment = firstFragment;

        //给radioGroup设置监听事件
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {

                    case R.id.first_radio_button:
                        switchFragment(firstFragment);
                        //viewPager.setCurrentItem(1);
                        break;


                    case R.id.second_radio_button:
                        switchFragment(secondFragment);
                        // viewPager.setCurrentItem(2);
                        break;


                    case R.id.third_radio_button:
                        switchFragment(thirdFragment);
                        //viewPager.setCurrentItem(3);
                        break;


                    case R.id.forth_radio_button:
                        switchFragment(forthFragment);
                        //viewPager.setCurrentItem(3);
                        break;
                }

            }
        });
    }

    private void switchFragment(Fragment fragment) {
        //判断当前显示的Fragment是不是切换的Fragment
        if (mFragment != fragment) {
            //判断切换的Fragment是否已经添加过
            if (!fragment.isAdded()) {
                //如果没有，则先把当前的Fragment隐藏，把切换的Fragment添加上
                getSupportFragmentManager().beginTransaction().hide(mFragment)
                        .add(R.id.main_fragment, fragment).commit();
            } else {
                //如果已经添加过，则先把当前的Fragment隐藏，把切换的Fragment显示出来
                getSupportFragmentManager().beginTransaction().hide(mFragment).show(fragment).commit();
            }
            mFragment = fragment;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        final IntentFilter intentFilter = new IntentFilter();
        m_Broadcastname = "com.barcode.sendBroadcast";
        intentFilter.addAction(m_Broadcastname);
        WarehouseSendGoodsActivity.this.registerReceiver(receiver, intentFilter);
    }


    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            if (arg1.getAction().equals(m_Broadcastname)) {
                String barCode = arg1.getStringExtra("BARCODE");

                if (firstFragment != null && firstFragment.isVisible()) {
                    callBackFirst.getResult(barCode);
                } else if (secondFragment != null && secondFragment.isVisible()) {
                    callBackSecond.getResult(barCode);
                } else if (thirdFragment != null && thirdFragment.isVisible()) {
                    callBackThird.getResult(barCode);
                } else if (forthFragment != null && forthFragment.isVisible()) {
                    callBackForth.getResult(barCode);
                }
            }
        }
    };

    public void onStart() {
        // TODO Auto-generated method stub
        //Scanner.m_handler = mHandler;
        Scanner.InitSCA();
        super.onStart();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (event.getRepeatCount() == 0) {
            if (keyCode == 4) {
                stopDialog();
            } else if ((keyCode == 221 || keyCode == 220)) {
            }
        }
        return true;
    }

    private void stopDialog() {
        final Dialog dialog = new Dialog(WarehouseSendGoodsActivity.this);
        View dialogView = LayoutInflater.from(WarehouseSendGoodsActivity.this)
                .inflate(R.layout.stop_dialog, null);
        Button btnSure = (Button) dialogView.findViewById(R.id.btn_sure_dialog);
        Button btnCancel = (Button) dialogView.findViewById(R.id.btn_cancel_dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(dialogView);
        dialog.setCancelable(false);

        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                DBUtils.updateAllSendGoodsState(WarehouseSendGoodsActivity.this);
                finish();
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        dialog.show();
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth()); //设置宽度
        dialog.getWindow().setAttributes(lp);
    }

    /*接口*/
    public interface CallBackFirst {
        /*定义一个获取信息的方法*/
        public void getResult(String result);
    }

    /*接口回调*/
    public void getData(CallBackFirst callBack) {
    /*获取文本框的信息,当然你也可以传其他类型的参数,看需求咯*/
        //String msg = ;
        this.callBackFirst = callBack;
        //callBack.getResult(result);
    }


    /*接口*/
    public interface CallBackSecond {
        /*定义一个获取信息的方法*/
        public void getResult(String result);
    }

    /*接口回调*/
    public void getData(CallBackSecond callBack) {
    /*获取文本框的信息,当然你也可以传其他类型的参数,看需求咯*/
        //String msg = ;
        this.callBackSecond = callBack;
        //callBack.getResult(result);
    }


    /*接口*/
    public interface CallBackThird {
        /*定义一个获取信息的方法*/
        public void getResult(String result);
    }

    /*接口回调*/
    public void getData(CallBackThird callBack) {
    /*获取文本框的信息,当然你也可以传其他类型的参数,看需求咯*/
        //String msg = ;
        this.callBackThird = callBack;
        //callBack.getResult(result);
    }


    /*接口*/
    public interface CallBackForth {
        /*定义一个获取信息的方法*/
        public void getResult(String result);
    }

    /*接口回调*/
    public void getData(CallBackForth callBack) {
    /*获取文本框的信息,当然你也可以传其他类型的参数,看需求咯*/
        //String msg = ;
        this.callBackForth = callBack;
        //callBack.getResult(result);
    }
}
